<?php
namespace Home\Controller;
use Lib\Alidayu\SendSMS;
class OrderController extends CommonController {

public function _initialize()
{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("order");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
}

//创建订单
public function order_add()
{
	// echo RedisDelete('Accesstoken');
	// echo $this->GetAccessToken();
	// echo RedisGet('Accesstoken');
		// $labeldata = M("label")->select();//读取配置
		$medialist = M("media")->field('id,media_name')->select();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('id desc')->select();
		$systemdata = M("systemsetup")->where(array("type"=>0))->select();
		// p($systemdata);exit();
		// $this->assign('labeldata',$labeldata);
		$this->assign('medialist',$medialist);
		$this->assign('leveldata',$leveldata);
		$this->assign('systemdata',$systemdata);
		$this->display();
}
public function media_ajax(){
	$media_id = I('get.media_id');
	$data['media'] = M("media")->field('mmid')->where("id=$media_id")->find();
	$mmid = $data['media']['mmid'];
	$data['special'] = M("mediaspecial")->field('id,specialname')->where("mediaid=$mmid")->select();
	exit(json_encode($data));
}
// 生成订单

// 生成订单
public function CreateOrder()
{
	// $_POST['token'] = "c4ca4238a0b923820dcc509a6f75849b";
	// $_POST['title'] = "article";
	// $_POST['article'] = "article";
	// $_POST['email'] = "article@email.com";
	// $_POST['day'] = "0";
	// $_POST['hour'] = "19";
	// $_POST['minute'] = "55";
	// $_POST['demand'] = "3";
	// 1.接受参数
	$sub_type = 1;
	if (I('post.ordertype') == 0) {
		$sub_type = 2;//是否为新手订单
	}elseif (I('post.ordertype') == 1) {
		$sub_type = 1;//是否为虚拟订单
	}
	$media = !empty(I('post.media')) ? I('post.media') : $this->error('自媒体不能为空',"",1);
	$mediainfo = M("media")->where("id=$media")->find();
	empty($mediainfo) ? $this->error('自媒体无效',"",1) : "" ;
	// $demand = I('post.demand',1,'intval');//1极速配音 2加急 3预约配音
	$soundtype = I('post.soundtype',1,'intval');//1讲述 2情感 3演绎 4新闻 5英文
	$sex = I('post.sex',0,'intval');//0全部  1男  2女
	$speed = I('post.speed',1,'intval');//1正常 2快 3慢
	$level = I('post.level',7,'intval');//5A 6B 7C
	$title = !empty(I('post.title')) ? I('post.title') : $this->error('标题不能为空',"",1);
	$article = !empty(I('post.article')) ? I('post.article') : $this->error('文章内容不能为空',"",1);
	$article = htmlspecialchars(preg_replace("/<img[^>]*>/i"," ",html_entity_decode($article)));
	$email = $mediainfo['email'];
	$readtitle = I('post.readtitle',1,'intval');//是否读标题  0否  1是
	$now_time = time();
	$ordertiming = !empty(I('post.ordertiming')) ? strtotime(I('post.ordertiming')) : $now_time;
/*-----------------------------------------------*/
	$content = $this->PostOss($article);
	if (!$content) {$this->error('文章上传内容为空',"",1);}
	$systemdata = M("systemsetup")->where(array("type"=>0))->select();
	$mintime = $systemdata['9']['value'];
	$circulationtime = $systemdata['10']['value'];
	$perprice = 0;
	/*switch ($demand) {
		case '2':
			$mintime = $systemdata['6']['value'];
			$circulationtime = $systemdata['7']['value'];
			$perprice = $systemdata['8']['value'];
			break;
		case '3':
			$mintime = $systemdata['11']['value'];
			$circulationtime = $systemdata['12']['value'];
			$perprice = $systemdata['13']['value'];
			break;
	}*/
	$max_mun = 100;
	$wordnumber = $this->fnGetCpmisWords(html_entity_decode($article));//文章字数
	$wordnum = $wordnumber<500?500:$wordnumber;//字数最少500字
	$expectedtime = ceil(ceil($wordnumber/$max_mun)*$mintime/60)*60;//预计用时
	$user_num = ceil($wordnum/$max_mun);
	$levelmoney = M('systemsetup')->where(array("type"=>1,"id"=>$level))->find()['money'];
	$moneynumber = $levelmoney*$user_num;//原价
	$usemoney = 0;//变价
	$largestnumber = $systemdata['2']['value'];
	/*switch ($demand) {
		case '2':
			$usemoney = $moneynumber*($perprice/100);
			break;
		case '3':
			$usemoney = $moneynumber*($perprice/100);
			break;
	}*/

	$db_systemorder = D("Systemorder");
	$data['media_id'] = $mediainfo['id'];
	$data['media_name'] = $mediainfo['media_name'];
	$data['media_pic'] = $mediainfo['media_pic'];
	$data['media_tel'] = $mediainfo['media_tel'];
	$data['media_message'] = $mediainfo['media_message'];
	$data['type'] = 1;
	$data['sub_type'] = $sub_type;
	$data['requirement'] = 1;
	$data['title'] = $title;
	$data['content'] = $content;
	$data['words'] = $wordnumber;
	$data['category'] = $soundtype;
	$data['speed'] = 1;
	$data['sex'] = $sex;
	$data['level'] = 7;
	$data['is_read_title'] = $readtitle;
	// 如果是预约单 单价要减掉变价
	$data['price'] = $moneynumber;
	$data['increase_price'] = 0;
	$data['max_join_num'] = $largestnumber;
	$data['deliver_time'] = $expectedtime+$circulationtime;
	$data['add_time'] = date("YmdH",$now_time);
	$data['createtime'] = $now_time;
	$data['snatch_time'] = $systemdata['3']['value'];
	$data['status'] = 1;
	$data['email'] = $email;
	$data['ordertiming'] = $ordertiming;
	if (!$db_systemorder->create($data)){//自动验证
		$this->error('参数错误',"",1);
	}else{
		if ($sub_type == 1) {
			$result = $db_systemorder->add($data);
			if($result){
				$this->adminlog("创建虚拟单".$result);
				$this->success('支付成功，已通知主播接单',"/Home/Order/order_list",1);
			}else{
				$this->error('数据异常',"",1);
			}
		}else{
			$res = $db_systemorder->add($data);
			if($res){
				$this->adminlog("创建新人单".$res);
				$this->success('下单成功,不用支付',"/Home/Order/order_list",1);
			}else{
				$this->error('数据异常',"",1);
			}
		}
	}
}
// 再次编辑订单
/*public function edit_article(){
	$oid = I('get.oid');
	$articledata = M('order')->where("id = $oid")->find();
	$content = $this->GetOss($articledata['content']);
	$article = str_replace(array("(",")"),array("（","）"),$content);
	$leftarr = explode("（", $article);
	$num = 0;
	foreach ($leftarr as $key1 => &$value1) {
		if ($key1 != 0) {
			$str = "）&lt;/font&gt;&lt;a class=a".$key1." id=del".$key1." onclick='delTime(this)'&gt;&lt;img src='/Public/admin/sncss/images/delete.png'&gt;&lt;/a&gt;";
			$value1 = str_replace("）",$str,$value1);
			$value1 = "&lt;font color=#ff0001 class=a".$key1."&gt;（".$value1;
		}
		$num = $key1;
	}
	$articledata['article'] = implode("", $leftarr);
	$articledata['article'] = html_entity_decode($articledata['article']);
	// p($articledata);exit();
	$this->assign('articledata',$articledata);
	$this->assign('newsingle',$newsingle);
	$this->assign('num',$num);
	$this->display();
}*/
// 二次编辑跳转
/*public function next_order(){
	// p($_POST);exit();
	$oid = I('post.oid');
	$article = I('post.article');
	$newsingle = I('post.newsingle');
	$content = $this->PostOss($article);

	$article = str_replace("#ff0001","",$article);
	// p($article);exit();
	$db_order = M("order");
	$db_order->where(array('id'=>$oid))->save(array("content"=>$content));//更新数据
	// $o = $db_order->field('id,mid,is_collect,number,level')->where("id = $oid")->find();
	$this->success('下单成功，请支付',"/Home/Order/order_list",1);
}*/
// 支付订单
/*public function confirm_pay(){
	$oid = I('get.oid');
	$signsystem = M("systemsetup")->where(array("type"=>0))->select();
	$db_order = M("order");
	$data['is_pay'] = 1;
	$data['releasetime'] = time();
	$res = $db_order->where(array('id'=>$oid))->save($data);//更新数据
	$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
	$orderdata = M("order")->where(array("id"=>$oid))->find();
	if ($res) {
		$res = $this->order_push($orderdata,$signsystem,$leveldata);
	}
				// var_dump($res);exit();
	// echo RedisGet('Accesstoken');
	$this->success('支付成功，已通知主播接单',"/Home/Order/order_list",1);
}*/

/*public function cancel_pay(){
	$oid = I('get.oid');
	$db_order = M("order");
	$data['status'] = 0;
	$db_order->where(array('id'=>$oid))->save($data);//更新数据
	$this->success('取消成功',"/Home/Order/order_list",1);
}*/

//重新下单
/*public function order_reorder()
{
		$oid = I('get.oid');
		$db_order = M("order");
		$data['status'] = 0;
		$db_order->where(array('id'=>$oid))->save($data);//更新数据
		$orderdata = $db_order->where(array("id"=>$oid))->find();
		$articledata = $this->GetOss($orderdata['content']);
		$medialist = M("media")->field('id,media_name')->select();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$orderdata['ptime'] = round(($orderdata['expectedtime']+$orderdata['circulationtime']+$orderdata['sleep']+$orderdata['overrulesum'])/60);
		switch ($orderdata['demand']) {
				case '1':$orderdata['usemoney'] = sprintf('%.2f', $orderdata['money']);break;
				case '2':$orderdata['usemoney'] = sprintf('%.2f', $orderdata['money']+$orderdata['premium']);break;
				case '3':$orderdata['usemoney'] = sprintf('%.2f', $orderdata['money']);break;
				default:$orderdata['usemoney'] = sprintf('%.2f', $orderdata['money']);break;
			}
		$mid = $orderdata['mid'];
		$mmid = M("media")->where(array("id"=>$mid))->field('mmid')->find()['mmid'];
		$specialdata = M("mediaspecial")->field('id,specialname')->where("mediaid=$mmid")->select();
		$systemdata = M("systemsetup")->where(array("type"=>0))->select();
		$this->articledata = $articledata;
		// p($orderdata);
		// p($specialdata);exit();
		$this->assign('orderdata',$orderdata);
		$this->assign('leveldata',$leveldata);
		$this->assign('specialdata',$specialdata);
		$this->assign('systemdata',$systemdata);
		$this->display();
}*/

//新订单列表
public function order_list()
{
		// p($_POST);exit();
		// 获取redis缓存数据
		// $data = json_decode(RedisGet('wechat'),true);
		$db_order = M("order o");
		$soundtype = I('get.soundtype');
		$ordertype = I('get.ordertype');
		$order_name = I('get.order_name');
		$levelkey = I('get.levelkey');
		$order_status = I('get.order_status');
		// $mid = I('get.mid')?I('get.mid'):0;
		$this->soundtype = $soundtype;
		$this->ordertype = $ordertype;
		$this->order_name = $order_name;
		$this->levelkey = $levelkey;
		$this->order_status = $order_status;
		$where = "o.pay_status=1 and o.status!=0 and o.sub_type!=1 and o.type=1 ";
		if (!empty($soundtype)) {$where .= " and o.category = \"".($soundtype)."\" ";}
		if (!empty($levelkey)) {$where .= " and `level` = \"".$levelkey."\" ";}
		if (!empty($order_name)) {$where .= " and (media_name like '%$order_name%' or o.id like '%$order_name%' or title like '%$order_name%')";}
		// if (!empty($mid)) {$where .= " and (media_id = $mid)";}
		switch ($ordertype) {
			case '1':$where .= " and (bgm_id != 0)";break;
			case '2':$where .= " and (o.sub_type = 3)";break;
		}
	switch ($order_status) {//1作废 2待抢单 3待交付 4已交付未上传WAV 5已交付未结算 6已评价未传wav 7已交付已结算 8需要重新上传音频 9无人接单 10超时未能完成 11用户主动取消订单
		case '1':$where .= " and (o.status = 0)";break;
		case '2':$where .= " and (o.status = 1)";break;
		case '3':$where .= " and (o.status = 3)";break;
		case '4':$where .= " and (o.status = 5)";break;
		case '5':$where .= " and (o.status = 6)";break;
		case '6':$where .= " and (o.status = 7)";break;
		case '7':$where .= " and (o.status = 8)";break;
		case '8':$where .= " and (o.status = 9)";break;
		case '9':$where .= " and (o.status = 11)";break;
		case '10':$where .= " and (o.status = 13)";break;
		case '11':$where .= " and (o.status = 15)";break;
	}
		// echo $where;exit();
		$count = $db_order->where($where)->order('o.id desc')->count();
		$p = getpage($count, 20);
		$orderlist = $db_order->where($where)->join('__SYSTEMSETUP__ s ON o.level = s.id','LEFT')->join('__BGM__ b on b.id = o.bgm_id','left')->join('__GET_ORDER_LOG__ l on o.id = l.oid','left')->field('o.*,s.message level_type,b.audio,l.num')->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		// echo $db_order->_sql();exit();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		// $labeldata = M("label")->select();//读取配置
		foreach ($orderlist as $key => &$value) {
//			$value['attempts'] = !empty($data[$value['id']]['count'])?$data[$value['id']]['count']:"";
//			$value['ordernum'] = $db_order->where(array("media_id"=>$value['media_id']))->where("(status=1 and pay_status=1) or status=3 or status=5 or status=7 or status=9")->count()+0;
			$value['usemoney'] = $value['price'];
			if($value['requirement']==3) {
				$value['usemoney'] = $value['price']+$value['increase_price'];
			}
			$value['usemoney'] = $value['bgm_id']!=0?($value['usemoney']*2+20):($value['usemoney']*2);
			switch ($value['pay_type']) {
				case '1':$value['pay_type'] = "微信";break;
				case '2':$value['pay_type'] = "支付宝";break;
				case '5':$value['pay_type'] = "余额";break;
				default:$value['pay_type'] = "余额";break;
			}
		}
		// p($orderlist);exit();
		$page = $p->show();
		// 获取查询参数
		$map['order_name'] = empty($order_name)?"":$order_name;
		$map['soundtype'] = $soundtype;
		$map['ordertype'] = $ordertype;
		$map['levelkey'] = $levelkey;
		$map['order_status'] = $order_status;
		$p->parameter = "";
		foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($p);exit();
		$this->assign('orderlist',$orderlist);
		// $this->assign('labeldata',$labeldata);
		$this->assign('leveldata',$leveldata);
		$this->assign('page', $page);
		$this->display();
}
/*
//新订单列表
public function passorder_list()
{
		// p($_POST);exit();
		$db_order = M("order o");
		$soundtype = I('post.soundtype');
		$order_name = I('post.order_name');
		$levelkey = I('post.levelkey');
		$this->soundtype = $soundtype;
		$this->order_name = $order_name;
		$this->levelkey = $levelkey;
		$where = "status=5 ";
		if (!empty($soundtype)) {$where .= " and o.type = \"".($soundtype-1)."\" ";}
		if (!empty($levelkey)) {$where .= " and `level` = \"".$levelkey."\" ";}
		if (!empty($order_name)) {$where .= " and (media_name like '%$order_name%' or id like '%$order_name%' or title like '%$order_name%')";}
		// echo $where;exit();
		$count = $db_order->where($where)->join('__MEDIA__ m ON o.mid = m.id')->join('__SYSTEMSETUP__ s ON o.level = s.id')->field('o.*,m.media_name,s.message level_type')->order('o.id desc')->count();
		$p = getpage($count, 20);
		$orderinfo = $db_order->where($where)->join('__MEDIA__ m ON o.mid = m.id')->join('__SYSTEMSETUP__ s ON o.level = s.id')->field('o.*,m.media_name,s.message level_type')->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$labeldata = M("label")->select();//读取配置
		foreach ($orderinfo as $key => &$value) {
			$value['ordernum'] = $db_order->where(array("mid"=>$value['mid']))->where("(status=1 and is_pay=1) or status=2 or status=3 or status=4 or status=5")->count()+0;
			switch ($value['demand']) {
				case '1':$value['usemoney'] = $value['money'];break;
				case '2':$value['usemoney'] = $value['money']+$value['premium'];break;
				case '3':$value['usemoney'] = $value['money'];break;
				default:$value['usemoney'] = $value['money'];break;
			}
			$value['usemoney'] = $value['bgmid']!=0?($value['usemoney']*2+20):($value['usemoney']*2);
			switch ($value['status']) {
				case '3':$value['paytype'] = "余额";break;
				case '4':$value['paytype'] = "微信";break;
				case '5':$value['paytype'] = "支付宝";break;
				default:$value['paytype'] = "不知道";break;
			}
		}

		// p($orderinfo);exit();
		$this->assign('orderinfo',$orderinfo);
		$this->assign('labeldata',$labeldata);
		$this->assign('leveldata',$leveldata);
		$this->assign('page', $p->show());
		$this->display();
}*/
//假订单列表
public function dummyorder_list()
{
		// p($_POST);exit();
		$db_order = M("order o");
		$soundtype = I('post.soundtype');
		$order_name = I('post.order_name');
		$levelkey = I('post.levelkey');
		$this->soundtype = $soundtype;
		$this->order_name = $order_name;
		$this->levelkey = $levelkey;
		$where = "sub_type=1 ";
		if (!empty($soundtype)) {$where .= " and `category` = \"".($soundtype)."\" ";}
		if (!empty($levelkey)) {$where .= " and `level` = \"".$levelkey."\" ";}
		if (!empty($order_name)) {$where .= " and (media_name like '%$order_name%' or o.id like '%$order_name%' or title like '%$order_name%')";}
		// echo $where;exit();
		$count = $db_order->where($where)->order('o.id desc')->count();
		$p = getpage($count, 20);
		$orderlist = $db_order->where($where)->join('__SYSTEMSETUP__ s ON o.level = s.id','LEFT')->field('o.*,s.message level_type')->order('o.id desc')->limit($p->firstRow, $p->listRows)->select();
		// echo $db_order->_sql();exit();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		// $labeldata = M("label")->select();//读取配置
		foreach ($orderlist as $key => &$value) {
			$value['attempts'] = !empty($data[$value['id']]['count'])?$data[$value['id']]['count']:"";
			$value['ordernum'] = $db_order->where(array("media_id"=>$value['mid']))->where("(status=1 and pay_status=1) or status=3 or status=5 or status=7 or status=9")->count()+0;
			$value['usemoney'] = $value['price'];
			if($value['requirement']==3) {
				$value['usemoney'] = $value['price']+$value['increase_price'];
			}
			$value['usemoney'] = $value['bgm_id']!=0?($value['usemoney']*2+20):($value['usemoney']*2);
			switch ($value['pay_type']) {
				case '1':$value['pay_type'] = "微信";break;
				case '2':$value['pay_type'] = "支付宝";break;
				case '5':$value['pay_type'] = "余额";break;
				default:$value['pay_type'] = "余额";break;
			}
		}
		// p($orderlist);exit();
		$this->assign('orderlist',$orderlist);
		$this->assign('labeldata',$labeldata);
		$this->assign('leveldata',$leveldata);
		$this->assign('page', $p->show());
		$this->display();
}


//订单详情
public function order_edit()
{
		$oid = I('get.oid');
		$db_order = M("order");
		$labeldata = M("label")->select();//读取配置
		// $db_member = M("member");
		$orderdata = $db_order->where(array("id"=>$oid))->find();
		$articledata = $this->GetOss($orderdata['content']);
		$articledata = htmlspecialchars(str_replace("spanstyle=","span style=",html_entity_decode($articledata)));
		$medialist = M("media")->field('id,media_name')->select();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		$orderdata['plantime'] = round(($orderdata['deliver_time']+$orderdata['delay_time'])/60);
		$orderdata['endtime'] = date("Y-m-d H:i:s",($orderdata['pay_time']+$orderdata['deliver_time']+$orderdata['delay_time']));
		$orderdata['user_money'] = $orderdata['price'];
		if($orderdata['requirement']==3) {
			$orderdata['user_money'] = $orderdata['price']+$orderdata['increase_price'];
		}
		if ($orderdata['bgm_id'] != 0) {
			$bgmmoney = 10;
		}
		$orderdata['user_money'] = $orderdata['user_money']+$bgmmoney;
		$orderdata['media_money'] = $orderdata['user_money']*2;
		if($orderdata['type'] == 2){//签约单走字数，金额全为0
			$orderdata['user_money'] = 0;
			$orderdata['media_money'] = 0;
		}
		$special = $orderdata['special'];
		$mid = $orderdata['media_id'];
		$mmid = M("media")->where(array("id"=>$mid))->field('mmid')->find()['mmid'];
		$specialdata = M("mediaspecial")->field('id,specialname')->where("mediaid=$mmid")->select();
		if (!empty($orderdata['audio_link'])) {
			if (strpos($orderdata['audio_link'], "audio.heardtech.com") === false) {
				$orderdata['audio_link'] = "http://audio.heardtech.com/".$orderdata['audio_link'];
			}
		}
		$orderdata['wav_time'] = M('musictime')->where(array('oid'=>$orderdata['id'],"type"=>1))->find()['musictime'];
		// $orderdata['mp3'] = M('musictime')->where(array('oid'=>$orderdata['id']))->find()['music'];
		// p($orderdata);exit();
		$this->assign('labeldata',$labeldata);
		// $this->assign('medialist',$medialist);
		$this->assign('orderdata',$orderdata);
		$this->assign('articledata',$articledata);
		$this->assign('leveldata',$leveldata);
		$this->assign('specialdata',$specialdata);
		$this->display();
}
public function edit_order(){
		// p($_POST);exit();
	/*$oid = I('post.oid');
	$special = I('post.special');
	$db_order = M("order");
	$data['special'] = $special;
	$db_order->where(array('id'=>$oid))->save($data);//更新数据
	$this->success('专辑修改成功',"/Home/Order/order_list",1);*/
	$uuid = I('post.uuid');
	$userleabl = I('post.userleabl');
	M("userleabllog")->add(array("uuid"=>$uuid,"userleabl"=>$userleabl));
	$this->success('添加成功',"/Home/Order/order_list",1);
}
public function editarticle(){
	$oid = I('post.oid');
	$article = I('post.article');
	$content = $this->PostOss($article);
	if (!$content) {$this->error('文章上传内容为空',"",1);}
	M("order")->where(array('id'=>$oid))->save(array("content"=>$content));
	$this->adminlog("修改订单文章内容：".$oid);
	$this->success('修改成功',"/Home/Order/order_list",1);
}
//订单评价
public function order_score()
{
		$oid = I('get.oid');
		$db_order = M("order");
		$orderdata = $db_order->where(array('id'=>$oid,'status'=>['in',[5,6]]))->find();
		if (!$orderdata) {
			$this->error('订单无效',"",1);
		}
		if (!empty($orderdata['audio_link'])) {
			if (strpos($orderdata['audio_link'], "audio.heardtech.com") === false) {
				$orderdata['audio_link'] = "http://audio.heardtech.com/".$orderdata['audio_link'];
			}
		}
		$this->assign('jumpurl',$_SERVER["HTTP_REFERER"]);
		$this->assign('orderdata',$orderdata);
		$this->display();
}

//评价订单
public function score_order(){
		// p($_POST);exit();
	// $_POST['oid'] = 121;
	$oid = I('post.oid');
	$score = I('post.score');
	$passmessage = I('post.passmessage');
	$db_order = M("order");

	$db_order = M("order");
	$order = $db_order->where(array('id'=>$oid,'status'=>['in',[5,6]]))->find();
	if ($score == 0 || empty($score)) {
		$this->ApiError('评分无效','900005');
	}
	/*算价*/
	$money = moneyzero($order['price']);
	if ($order['requirement'] == 3) {
		$money = moneyzero($order['price']+$order['increase_price']);
	}
	if ($order['bgm_id'] != 0) {
		$money = $money+10;
	}
	if ($order['type'] == 2) {
		$money = 0;
	}
	/*加分*/
	switch ($score) {
		case '1':$num=1;break;
		case '2':$num=1;break;
		case '3':$num=1;break;
		case '4':$num=1;break;
		case '5':$num=2;break;
	}
	if ($order['type']==3 || $order['type']==4) {
		$num=0;
	}else{
		if ($num!=0) {
			$memberinfo = M("member")->where(array('uuid'=>$order['user_id']))->find();
			if ($memberinfo['score']<100) {
				if ($memberinfo['score']>98&&$num==2) {
					$num = 1;
				}
				M("member")->where(array('uuid'=>$order['user_id']))->setInc('score',$num);// 加信用分
				M('score')->add(array("uuid"=>$order['user_id'],"score"=>$num,"time"=>time(),"describe"=>"完成订单加分","type"=>1));
			}else{
				$num = 0;
			}
		}
		$tmpdata['scorestar'] = $score;
		$tmpdata['scorenum'] = $num;
		$tmpdata['scoremessage'] = $passmessage;
		$res = A("Template")->ordersendmessage($order['user_id'],3,$order,$tmpdata);
		$userordernum = $db_order->where(array('uuid'=>$order['user_id'],'state'=>8))->count()+0;
		if ($userordernum > 0) {
			M('member')->where(array('uuid'=>$order['user_id'],'ordertype'=>0))->save(array('ordertype'=>1));
		}
	}
	if ($order['status'] == 5) {
		$data['score'] = $score;
		$data['status'] = 7;
		$db_order->where(array('id'=>$order['id']))->save($data);//更新数据
		$wavtype = 6;//继续传wav
	}elseif ($order['status'] == 6) {
		$data['score'] = $score;
		$data['status'] = 8;
		$row = $db_order->where(array('id'=>$order['id']))->save($data);//更新数据
		if ($row) {
			$musictime = M('musictime')->where(array('oid'=>$order['id'],'type'=>1))->find()['musictime']+0;
			M("member")->where(array('uuid'=>$order['user_id']))->setInc('alwaystime',$musictime);// 加总时长
			M("member")->where(array('uuid'=>$order['user_id']))->setInc('monthtime',$musictime);// 加剩余时长
			M("member")->where(array('uuid'=>$order['user_id']))->setInc('income',$money);// 加总收入
			M("member")->where(array('uuid'=>$order['user_id']))->setInc('balance',$money);// 加余额
			M('income')->where(array('oid'=>$order['id'],'uuid'=>$order['user_id']))->save(array('status'=>1));
			M('musictime')->where(array('oid'=>$order['id'],'uuid'=>$order['user_id']))->save(array('state'=>1));
			if ($order['type']==1 || $order['type']==2) {
				$tmpdata['price'] = $money;
				$res = A("Template")->ordersendmessage($order['user_id'],6,$order,$tmpdata);
			}
		}
		$wavtype = 7;
	}
	$socketdata['type'] = "ScoreOrder";
	$socketdata['id'] = $oid;
	$socketdata['status'] = $wavtype;
	$socketdata['scorenum'] = $score;
	$socketdata['orderprice'] = $money;
	$socket = new \Home\Controller\SocketController();
	$socket->send_to_uid($order['user_id'],$socketdata);
	$this->adminlog("评价订单：".$oid.":".$passmessage);
	$jumpurl = I('post.jumpurl');
	$this->success('评分成功',$jumpurl,1);
}
public function order_pass(){
	// p($_POST);exit();
	$oid = I('post.oid');
	$passmessage = I('post.passmessage');
	$db_order = M("order");
	$data['status'] = 9;
	$data['note'] = $passmessage;
	$db_order->where(array('id'=>$oid))->save($data);//更新数据
	// $db_orderverify->where(array('oid'=>$oid))->save(array('successful'=>0,'isremind'=>0));//更新数据
	$order = $db_order->where(array('id'=>$oid))->find();
	// $orderverify = $db_orderverify->where(array('oid'=>$oid))->find();
	$now_time = time();
	if ($order['pay_time']+$order['deliver_time']+$order['delay_time']<$now_time) {
		$time = $now_time-($order['pay_time']+$order['deliver_time']+$order['delay_time'])+3600;
	}else{
		$time = 3600;
	}
	$db_order->where(array('id'=>$oid))->setInc('delay_time',$time);// 驳回加30分钟
	$data_message['scoremessage'] = $passmessage;
	$res['wechat'] = A("Template")->ordersendmessage($order['user_id'],4,$order,$data_message);
	// $this->postsocket($oid);
	$socketdata['message'] = $passmessage;
	$socketdata['type'] = "RejectOrder";
	$socketdata['id'] = $oid;
	$socket = new \Home\Controller\SocketController();
	$socket->send_to_uid($order['user_id'],$socketdata);
	M('musictime')->where(array('oid'=>$oid))->save(array('state'=>2));
	// M('musictime')->where("oid=$oid")->delete();
	$tel = M('member')->field('username')->where(array('uuid'=>$order['user_id']))->find()['username'];
	// $res['alidayu'] = $this->sendAlidayu($tel,"SMS_117115044");
	$mobile = $tel;
	$type = 7;
	$delay_time = ($order['pay_time']+$order['deliver_time']+$order['delay_time']) - time();
	$ypdata['message'] = ceil($delay_time/60);
	D("Sendsms")->yunpiansend($mobile,$type,$ypdata);
	$title = $order['media_name']."要求重新配音";
	$content = $order['media_name']."标题为《".$order['title']."》的订单表示无法验收,要求重新配音,理由为".$passmessage;
	switch ($order['type']) {
		case '1':$this->noticeemail($title,$content,12,$oid);break;
		case '2':$this->noticeemail($title,$content,13,$oid);break;
	}
	$data['uuid'] = $order['user_id'];
	$data['type'] = 2;
	$data['relate_id'] = $oid;
	$data['note'] = $passmessage;
	$data['date'] = time();
	$data['audio_url'] = $order['audio_link'];
	$data['wav_url'] = $order['wav_link'];
	M('verify_log')->add($data);
	$this->adminlog("驳回订单：".$oid);
	$jumpurl = $_SERVER["HTTP_REFERER"];
	$this->success('审核不通过',$jumpurl,1);
}
public function rejectorder_list()
{
		// p($_POST);exit();
		$order_name = I('get.order_name');
		$this->p = I('get.p')?I('get.p'):1;
		$this->order_name = $order_name;
		$where = "r.type = 2 ";
		if (!empty($order_name)) {$where .= " and (t.media_name like '%$order_name%' or t.id like '%$order_name%' or t.title like '%$order_name%' or t.user_id like '%$order_name%')";}
		$db_rejectorderlog = M("verify_log r");
		// echo $where;exit();
		$count = $db_rejectorderlog
		->where($where)
		->join('__ORDER__ t ON r.relate_id = t.id',LEFT)
		->order('r.id desc')->count();
		$p = getpage($count, 20);
		$loglist = $db_rejectorderlog
		->where($where)
		->join('__ORDER__ t ON r.relate_id = t.id',LEFT)
		->field('r.relate_id,r.date,r.note,t.title,r.id,r.audio_url,r.wav_url,r.message,t.user_id,t.media_name')
		->order('r.id desc')->limit($p->firstRow, $p->listRows)->select();
		// p($loglist);exit();
		$this->assign('loglist',$loglist);
		$this->assign('page', $p->show());
		$this->display();
}
public function rejectorder_message(){
	// p($_POST);exit();
	$rid = I('post.rid');
	$p = I('post.p');
	$message = I('post.ordermessage');
	$db_rejectorder = M("verify_log");
	$data['message'] = $message;
	$db_rejectorder->where(array('id'=>$rid))->save($data);//更新数据
	$rejectorder = $db_rejectorder->where(array('id'=>$rid))->find();
	$oid = $rejectorder['relate_id'];
	$orderdata = M("order")->where(array("id"=>$oid))->find();
	$title = "驳回订单添加备注";
	$content = $orderdata['media_name']."标题为《".$orderdata['title']."》驳回理由为".$rejectorder['note']."的订单已经处理，处理结果:".$message;
	$this->noticeemail($title,$content,15,$oid);
	$this->adminlog("驳回订单添加备注：".$rid);
	$this->success('添加备注',"/Home/Order/rejectorder_list?p=".$p,1);
}
/*public function rejectorder_list()
{
		// p($_POST);exit();
		$db_rejectorderlog = M("rejectorderlog r");
		// echo $where;exit();
		$count = $db_rejectorderlog
		->join('__ORDER__ t ON r.oid = t.id',LEFT)
		->order('r.id desc')->count();
		$p = getpage($count, 20);
		$loglist = $db_rejectorderlog
		->join('__ORDER__ t ON r.oid = t.id',LEFT)
		->field('r.oid,r.time,r.passmessage,t.title,t.id,t.user_id,t.media_name')
		->order('r.id desc')->limit($p->firstRow, $p->listRows)->select();
		// p($loglist);exit();
		$this->assign('loglist',$loglist);
		$this->assign('page', $p->show());
		$this->display();
}*/
// 获取链接文章内容
function getwebcontent(){
	// $_POST['user_url'] = "https://mp.weixin.qq.com/s?__biz=MzIwMjUwNTcyMg==&tempkey=OTMxX2d5aFpCOGRZSk1rSFNVa1VZQlo4WXQySmkzR2xPSnc2Zy1SOE9lSHNvd0hTWXQtcjZ1VGhLRmtKa2ZVZFZ6eUQ2N3ptWWZlRFBMMzg4MGxVYWFCZEtDWGhCaWViR1B0dFlxX3BGQURqRXhWOTg0Uzd2eldLTUpEYjJPT2VJamxWZ2VVZjZ5SlBlTXBGeG9sLUY2TjU1WFgzUEU3WFR5VUJNM2pnRFF%2Bfg%3D%3D&chksm=16dced6121ab6477882ef589bd703da86d8f16b1cdc9ea6dbc29d43c277113eb17b3f595fd63&scene=0&previewkey=NdtaEQzKwauk7r4x%252F92AoMNS9bJajjJKzz%252F0By7ITJA%253D#rd";
	$url = $_POST['user_url'];
	// $url = str_replace("https","http",$url);
	Vendor('phpQuery.phpQuery');
	$result = do_curl($url);
	$result = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $result);
	\phpQuery::newDocumentHTML($result);
	if (strpos($url, "mp.weixin.qq.com") !== false) {
		$title = pq('title')->text();
		$str = pq('#js_content')->html();
	}elseif (strpos($url, "shimo.im") !== false) {
		$shimo =  file_get_contents($url);
		\phpQuery::newDocumentHTML($shimo);
		$title = pq('title')->text();
		$str = pq('script');
		$str = strip_tags(html_entity_decode($str));
		$str = strstr($str,"text");
		$str = explode('attribs', strval($str))['0'];
		$str = substr($str,10);
		$str = substr($str,0,-5);
		$str = str_replace("\\\\n","<br />",$str);
		$str = str_replace("*","",$str);
	}elseif (strpos($url, "www.jianshu.com") !== false) {
		$title = pq('title')->text();
		$str = pq('.show-content')->html();
	}
	$data['title'] = trim($title);
	$data['message'] = $this->removecss($str);
	exit(json_encode($data));
}

//订单列表
public function email_list()
{
	// $AccessToken = empty(RedisGet('Accesstoken')) ? $this->GetAccessToken(): RedisGet('Accesstoken');
	// p($this->GetAccessToken());
	// p(RedisGet('Accesstoken'));
	// p(RedisDelete('Accesstoken'));
	// if (empty(RedisGet('Accesstoken'))) {
	// 	echo 1;
	// }else{
	// 	echo 2;
	// }
	// p($AccessToken);
	// exit();
		// p($_POST);exit();
		$db_email = M("email s");
		$email_name = I('post.email_name');
		$this->email_name = $email_name;
		$where = " 1=1 ";
		if (!empty($email_name)) {$where .= " and (media_name like '%$email_name%' or email like '%$email_name%' or title like '%$email_name%')";}
		// echo $where;exit();
		$count = $db_email->where($where)->order('id desc')->count();
		$p = getpage($count, 20);
		$emailinfo = $db_email->where($where)->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		// p($emailinfo);exit();
		$this->assign('emailinfo',$emailinfo);
		$this->assign('page', $p->show());
		$this->display();
}

//阿里大于
	public function orderAlidayu($tel="13091718183",$Code='SMS_72800028',$name,$media,$time)
	{
		$alidayu = new SendSMS();
		$result = $alidayu->send($recNum=$tel,$smsParam='{"name":"'.$name.'","media":"'.$media.'","time":"'.$time.'"}',$smsTemplateCode=$Code,$smsFreesignName='听说Heard');
		// p($result);
	}

	// 首次交付音频列表
	public function order_first()
	{
		$data = M('member m')
		->join('__ORDER__ t ON m.uuid = t.user_id')
		->where("t.status = 7")
		->field("m.uuid,m.nickname,t.audio_link")
		->select();
		$list = [];
		$uuidlist = [];
		foreach ($data as &$vo) {
			if(!in_array($vo['uuid'], $uuidlist)){
				array_push($list, $vo);
				array_push($uuidlist, $vo['uuid']);
			}
		}
		 // 得到数组元素个数
        $count=count($list);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page= new \Think\Page($count,20);
        $data= array_slice($data,$Page->firstRow,$Page->listRows);
        // 分页显示输出﻿
        $show= $Page->show();
        $this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}
//主播标签列表
public function userleabl_list()
{

		$db_userleabllog = M("userleabllog");
		$userleabl = I('post.userleabl');
		$this->userleabl = $userleabl;
		$where = " 1=1 ";
		if (!empty($userleabl)) {$where .= " and (userleabl like '%$userleabl%')";}
		// echo $where;exit();
		$count = $db_userleabllog->where($where)->order('id desc')->count();
		$p = getpage($count, 40);
		$list = $db_userleabllog->where($where)->order('id desc')->limit($p->firstRow, $p->listRows)->select();
		// p($list);exit();
		$this->assign('list',$list);
		$this->assign('page', $p->show());
		$this->display();
}
}